﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SignalR_HubTest
{
    public class UploadHub : Hub
    {
        public static readonly Dictionary<string, List<string>> Subscriptions = new();

        public async Task Subscribe(string[] types)
        {
            if (types == null || types.Length == 0) return;

            lock (Subscriptions)
            {
                Subscriptions[Context.ConnectionId] = types.ToList();
            }

            Console.WriteLine($"客户端 {Context.ConnectionId} 订阅了：{string.Join(",", types)}");
            await Task.CompletedTask;
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            lock (Subscriptions)
            {
                Subscriptions.Remove(Context.ConnectionId);
            }

            Console.WriteLine($"客户端断开：{Context.ConnectionId}");
            return base.OnDisconnectedAsync(exception);
        }
    }
}
